import App from 'next/app'

class MyApp extends App {
    constructor(props) {
        super(props);
        const { Component, pageProps, router } = props;
        this.state = { Component, pageProps, router };
    }
    async componentDidMount() {
        try {
            let res = await global.$.get({
                url: "setting/get",
            })
            if (res && res.code && res.code === 200) {
                let data = res.data;
                let { name, avatar, detail, subtitle, site_name } = data;
                this.setState({ userInfo: { name, avatar, subtitle, detail, site_name } });
            } else {
                this.setState({ userInfo: { name: '无名小卒', avatar: '', subtitle: '', detail: '', site_name: '漫悦' } });
            }
        }
        catch (err) {
            this.setState({ userInfo: { name: '无名小卒', avatar: '', subtitle: '', detail: '', site_name: '漫悦' } });
        }
    }
    static getDerivedStateFromProps(nextProps, prevState) {
        if (nextProps.Component !== prevState.Component
            || nextProps.pageProps !== prevState.pageProps
            || nextProps.router !== prevState.router) {
            return {
                Component: nextProps.Component,
                pageProps: nextProps.pageProps,
                router: nextProps.router
            };
        }
        return null;
    }


    render() {
        const { Component, pageProps, userInfo } = this.state;
        return <Component {...pageProps} userInfo={userInfo} />
    }

}

export default MyApp